
# SYSTEM_COMPILE=arm-linux, x86_64-linux-gnu ...
SYSTEM_COMPILE=x86_64-linux-gnu
GCC=$(SYSTEM_COMPILE)-gcc
GXX=$(SYSTEM_COMPILE)-g++
STRIP=$(SYSTEM_COMPILE)-strip
CFLAGS =-Wall -I./

MAKE_PWD=$(shell pwd)
COMPILE_TIME = $(shell date +"%Y%m%d%H%M%S")

WORK_SUB_DIRS=$(shell ls ./)

BUILD_ROOT_DIR=./builddir/
PROJ_WORK_VERSION=$(BUILD_ROOT_DIR)deviceVersionSave.txt

WORK_INCLUDE_DIRS=$(foreach n,$(WORK_SUB_DIRS),$(shell if [ -d $(n) ]; then echo -I./$(n); fi;))
# WORK_SUB_DIR_SRCS=$(foreach n,$(WORK_SUB_DIRS),$(shell if [ -d $(n) ]; then echo $(n)/*.cpp; fi;))
WORK_SUB_DIR_SRCS=$(foreach n,$(WORK_SUB_DIRS),$(shell if [ -d $(n) ]; then ls $(n)/*.c 2> /dev/null; fi;))
BUILD_SUB_DIRS=$(BUILD_ROOT_DIR)
BUILD_SUB_DIRS+=$(foreach n,$(WORK_SUB_DIRS),$(shell if [ -d $(n) ]; then echo $(BUILD_ROOT_DIR)$(n); fi;))
$(info WORK_SUB_DIR_SRCS = $(WORK_SUB_DIR_SRCS))
$(info WORK_INCLUDE_DIRS = $(WORK_INCLUDE_DIRS))

CFLAGS = -I./ $(WORK_INCLUDE_DIRS)

SRCS = $(wildcard *.c)
SRCS += $(WORK_SUB_DIR_SRCS)
TARGET = $(BUILD_ROOT_DIR)test
$(info srcs: $(SRCS))
OBJS = $(patsubst %.c,$(BUILD_ROOT_DIR)%.o,$(SRCS))
$(info objs: $(OBJS))

#$(COMPILE_INCLUDE) $(COMPILE_LIB) $(COMMON_OPT_FLAG)
all:makeBuildingDirs versionWrite $(OBJS)
	$(CC) -o $(TARGET) $(OBJS) $(CFLAGS) $(COMPILE_LIB) -O3
	# cp run.sh $(BUILD_ROOT_DIR)
	# $(STRIP) $(TARGET)
# -g -rdynamic
$(OBJS):$(BUILD_ROOT_DIR)%.o:%.c
	$(CC) -c $^ -o $@ $(CFLAGS) $(COMPILE_INCLUDE) $(COMMON_OPT_FLAG) 

makeBuildingDirs:
	#echo mkdir -p $(BUILD_SUB_DIRS)
	mkdir -p $(BUILD_SUB_DIRS)

versionWrite:
	@echo "----$(COMPILE_TIME)----"
	@echo device-V1.1.$(COMPILE_TIME) > $(PROJ_WORK_VERSION)

clean:
	#rm -rf *.o $(TARGET) $(OBJS) $(SMP_OBJS) $(PROJ_WORK_VERSION) $(BUILD_ROOT_DIR)
	rm -rf $(BUILD_ROOT_DIR)


